package com.example.myapplication;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ChatActivity extends AppCompatActivity {
    private List<Message> messageList;
    private MessageAdapter messageAdapter;
    private RecyclerView recyclerView;
    private EditText messageInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        String chatName = getIntent().getStringExtra("chat_name");

        // 设置标题栏
        TextView titleText = findViewById(R.id.chatTitle);
        titleText.setText(chatName);

        // 设置返回按钮
        ImageButton backButton = findViewById(R.id.backButton);
        backButton.setImageResource(R.drawable.ic_back); // 需要添加返回图标
        backButton.setOnClickListener(v -> finish());

        // 初始化消息列表
        messageList = new ArrayList<>();
        messageAdapter = new MessageAdapter(messageList);

        // 设置RecyclerView
        recyclerView = findViewById(R.id.messagesRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(messageAdapter);

        // 发送按钮点击事件
        messageInput = findViewById(R.id.messageInput);
        Button sendButton = findViewById(R.id.sendButton);
        sendButton.setOnClickListener(v -> sendMessage());
    }

    private void sendMessage() {
        String messageContent = messageInput.getText().toString().trim();
        if (!messageContent.isEmpty()) {
            // 获取当前时间
            String currentTime = new SimpleDateFormat("HH:mm", Locale.getDefault())
                    .format(new Date());

            // 创建新消息
            Message message = new Message(messageContent, true, currentTime);
            messageList.add(message);

            // 更新RecyclerView
            messageAdapter.notifyItemInserted(messageList.size() - 1);
            recyclerView.scrollToPosition(messageList.size() - 1);

            // 清空输入框
            messageInput.setText("");

            // 模拟接收回复消息
            simulateResponse();
        }
    }

    private void simulateResponse() {
        // 模拟延迟回复
        recyclerView.postDelayed(() -> {
            String currentTime = new SimpleDateFormat("HH:mm", Locale.getDefault())
                    .format(new Date());
            Message response = new Message("收到你的消息了！", false, currentTime);
            messageList.add(response);
            messageAdapter.notifyItemInserted(messageList.size() - 1);
            recyclerView.scrollToPosition(messageList.size() - 1);
        }, 1000);
    }
}